package com.wing.product.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.platform.feign.PlatformClient;
import com.wing.product.model.entity.ProductCategory;
import com.wing.product.model.entity.ProductDetail;
import com.wing.product.model.request.ProductDetailForm;
import com.wing.product.model.response.ProductDetailVO;
import com.wing.product.service.MostUsedKnifeListService;
import com.wing.product.service.ProductDetailService;
import com.wing.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Validated
@Api(value = "Admin-作废物料列表", tags = "#/productCab/cancellationMaterial")
@RestController("cancellationMaterialController")
@RequestMapping("cabProduct/admin/cancellationMaterial")
@RequiredArgsConstructor
@Slf4j
public class CancellationMaterialController {

    private final PlatformClient platformClient;
    private final EnterpriseClient enterpriseClient;

    private final ProductService productService;
    private final ProductDetailService productDetailService;
    @Autowired
    private  MostUsedKnifeListService mostUsedKnifeListService;


    @ApiOperation(value = "查询所有常用刀具", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "skuCode", value = "商品SKU编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "skuName", value = "商品SKU名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "productName", value = "商品名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "productBrandName", value = "商品品牌名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProductDetailVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "id") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String skuCode
            ,String skuName
            ,String productName
            ,String productBrandName
            ,String oldSkuCode
            ,String oldSpecCode
            , String startTime
            , String endTime
            , ProductCategory.CategoryType categoryType
            , ProductCategory.Type type
            ,String query
            , Long productTypeId
            , Long productCategoryId
    ){
        Page<ProductDetail> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order",StrUtil.toUnderlineCase(order));
        paramMap.put("skuCode",skuCode);
        paramMap.put("skuName",skuName);
        paramMap.put("productName",productName);
        paramMap.put("productBrandName",productBrandName);
        paramMap.put("oldSkuCode",oldSkuCode);
        paramMap.put("oldSpecCode",oldSpecCode);
        paramMap.put("startCreateTime",startTime);
        paramMap.put("endCreateTime",endTime);
        if(null != productTypeId){
            paramMap.put("productTypeId",productTypeId);
        }
        if(null != productCategoryId){
            paramMap.put("productCategoryId",productCategoryId);
        }
        if(null != categoryType){
            paramMap.put("categoryType",categoryType.getValue());
        }
        if(null != type){
            paramMap.put("type",type.getValue());
        }
        if (StringUtils.isNotBlank(query)){
            paramMap.put("query",query);
        }
        IPage<ProductDetailVO> list =mostUsedKnifeListService.findAllCancellationMaterial(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }




    @ApiOperation(value = "恢复sku信息", notes = "作废")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/disable")
    public JsonResult disable(@Validated(ProductDetailForm.Add.class) ProductDetailForm productDetailForm) {
        // 判断当前物料是否已存在库存数据
        ProductDetailVO detailVO = productDetailService.get4appById(productDetailForm.getId());
        /*if((null != detailVO.getQuantity() && detailVO.getQuantity() > 0) || (null != detailVO.getQuantityCab() && detailVO.getQuantityCab() > 0)){
            return JsonResult.fail("当前物料已有库存数据，不可删除");
        }*/
        ProductDetail productDetail=new ProductDetail();
        productDetail.setId(productDetailForm.getId());
        productDetail.setIsDeleted(false);
        if (mostUsedKnifeListService.updateById(productDetail)) {
            return JsonResult.success("恢复成功");
        }
        return JsonResult.fail("恢复失败");
    }



}

